package com.rongji.cms.tools.db.strategy;

import org.springframework.util.Assert;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yang
 * @date 2023/1/29 13:48
 * @description 工厂类
 * 这个适用spring工程，需要各策略类实现InitializingBean接口，在方法afterPropertiesSet()里注册到map里
 */

public class DbStrategyFactory {
    private static Map<String, DbStrategy> strategys = new ConcurrentHashMap<String, DbStrategy>();

    /**
     * 通过type获取相应的数据库service
     *
     * @param type
     * @return
     */
    public static DbStrategy getByDbType(String type) {
        return strategys.get(type);
    }


    /**
     * 添加到map里，初始化时候用到
     *
     * @param dbType
     * @param dbStrategy
     */
    public static void register(String dbType, DbStrategy dbStrategy) {
        Assert.notNull(dbType, "dbType can't be null");
        strategys.put(dbType, dbStrategy);
    }

}
